using Game.Tile;
using HCore.Unity.Core;
using UnityEngine;

namespace Test
{
    /// <summary>
    ///     <ul>
    ///         制作拼图件的步骤：
    ///         <li>第1步-将原始纹理设置为 Tile。</li>
    ///         <li>第2步-创建与原始纹理相同宽度和高度的新纹理，并以完全的透明度填充它。</li>
    ///         <li>第3步-根据曲线和直线设置新创建的纹理边界，具体取决于每个方向的 PosNegType。</li>
    ///         <li>第4步-应用洪水填充来填充此边界内的区域。洪水填充将根据原始图像的颜色设置新创建的纹理的颜色。</li>
    ///     </ul>
    /// </summary>
    public class TestTilesGen : MonoBehaviour
    {
        //------------测试值------------//
        private const int _PADDING = 4;
        private const int _TILE_SIZE = 20;
        public string ImageFilename;
        private Sprite mSprite;
        private Texture2D mTextureOriginal;
        private Tile mTile;

        private void Start()
        {
            CreateBaseTexture();
            // mTile = new Tile(mTextureOriginal);
            // mTile.DrawCurve(Tile.Direction.Up, Tile.PosNegType.Pos, Color.blue);
            // mTile.DrawCurve(Tile.Direction.Right, Tile.PosNegType.Pos, Color.blue);
            // mTile.DrawCurve(Tile.Direction.Down, Tile.PosNegType.Pos, Color.blue);
            // mTile.DrawCurve(Tile.Direction.Left, Tile.PosNegType.Pos, Color.blue);
            //
            // mTile.DrawCurve(Tile.Direction.Up, Tile.PosNegType.Neg, Color.red);
            // mTile.DrawCurve(Tile.Direction.Right, Tile.PosNegType.Neg, Color.red);
            // mTile.DrawCurve(Tile.Direction.Down, Tile.PosNegType.Neg, Color.red);
            // mTile.DrawCurve(Tile.Direction.Left, Tile.PosNegType.Neg, Color.red);
        }

        private void Update()
        {
            // 检测到空格键向下输入时，会触发一系列操作。首先，它调用mTile对象的HideAllCurves方法，有效地隐藏了之前绘制的所有曲线。
            // 然后，它使用GetRandomType函数生成随机类型和颜色对，并用获得的类型和颜色向上绘制一条曲线。随后，它向右、向下和向左方向重复这个过程，每次生成一个新的随机类型和颜色对，并相应地绘制一条曲线。
            if (Input.GetKeyDown(KeyCode.Space)) TestRandomCurves();
            else if (Input.GetKeyDown(KeyCode.F)) TestTileFloodFill();
        }

        private void TestTileFloodFill()
        {
            if (mTile != null)
            {
                mTile.DestroyAllCurves();
                mTile = null;
            }

            mTile = new Tile(_TILE_SIZE, _PADDING, 0, 0, () => mTextureOriginal);


            var typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Up, typeColor.Item1, typeColor.Item2);
            mTile.SetCurveType(Tile.Direction.Up, typeColor.Item1);

            typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Right, typeColor.Item1, typeColor.Item2);
            mTile.SetCurveType(Tile.Direction.Right, typeColor.Item1);

            typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Down, typeColor.Item1, typeColor.Item2);
            mTile.SetCurveType(Tile.Direction.Down, typeColor.Item1);

            typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Left, typeColor.Item1, typeColor.Item2);
            mTile.SetCurveType(Tile.Direction.Left, typeColor.Item1);

            mTile.Apply();

            // We will now set the texture finalCut to the sprite.
            var spriteRenderer = GetComponent<SpriteRenderer>();
            var sprite = GameUtils.CreateSprite(mTile.FinalCutTexture, new Rect(0, 0, mTile.FinalCutTexture.width, mTile.FinalCutTexture.height));

            spriteRenderer.sprite = sprite;
        }

        /// <summary>
        ///     在更新方法中，我们将创建随机创建的瓷砖与按空格键相关联。
        ///     每当用户按下空格键时，我们都会从更新中调用此方法。
        /// </summary>
        private void TestRandomCurves()
        {
            if (mTile != null)
            {
                mTile.DestroyAllCurves();
                mTile = null;
            }

            mTile = new Tile(_TILE_SIZE, _PADDING, 0, 0, () => mTextureOriginal);

            var typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Up, typeColor.Item1, typeColor.Item2);
            typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Right, typeColor.Item1, typeColor.Item2);
            typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Down, typeColor.Item1, typeColor.Item2);
            typeColor = GetRandomType();
            mTile.DrawCurve(Tile.Direction.Left, typeColor.Item1, typeColor.Item2);

            var spriteRenderer = GetComponent<SpriteRenderer>();
            spriteRenderer.sprite = mSprite;
        }

        /// <summary>
        ///     创建基础的纹理。
        ///     把地图加载出来，显示在画布上。
        /// </summary>
        private void CreateBaseTexture()
        {
            mTextureOriginal = Resources.Load<Texture2D>(ImageFilename);
            if (!mTextureOriginal.isReadable)
            {
                Debug.Log("Texture is nor readable");
                return;
            }

            var spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
            mSprite = GameUtils.CreateSprite(mTextureOriginal, new Rect(0, 0, mTextureOriginal.width, mTextureOriginal.height));
            spriteRenderer.sprite = mSprite;
        }


        /// <summary>
        ///     此函数将随机生成类型和颜色。
        /// </summary>
        /// <returns></returns>
        private static (Tile.PosNegType, Color) GetRandomType()
        {
            return Random.Range(0f, 1f) < 0.5f ? (Tile.PosNegType.Pos, Color.blue) : (Tile.PosNegType.Neg, Color.red);
        }
    }
}